package BlockR;

import BlockR.function.*;
import BlockR.model.RandomNumOBJ;
import cpabe.Common;
import cpabe.Cpabe;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Decrypt {
    public static byte[][][] decrypt(String encfile, int arraySize, RandomNumOBJ firstBlockKey) {
        byte[][][] allBlock = null;
        try {
            MappedBiggerFileReader.Read_File(encfile, arraySize);
            allBlock = MappedBiggerFileReader.getAll_Block();
//            allBlock = Cpabe.brBuf;
            if (allBlock == null) {
                throw new IOException();
            }
            BlockR.function.Output.output("D:\\test\\cpabe\\testtesttest.txt", allBlock);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("密文i/o失败");
        }
        RandomNumOBJ[] allRandomNum = new RandomNumOBJ[MappedBiggerFileReader.getBlock_index() + 1];
        allRandomNum[0] = firstBlockKey;
        assert allBlock != null;
        Decrypt_End_Piece.Do_Decrypt_End_Piece(allBlock, firstBlockKey, arraySize);
        int G = GetCipherCode.getG();
        assert firstBlockKey != null;
        allBlock = Decrypt_First_Block.Do_Decrypt_First_Block(allBlock, firstBlockKey, G, arraySize);
        allBlock = Decrypt_Other_Block.Do_Decrypt_Other_Piece(allBlock, allRandomNum, G, arraySize);
        return allBlock;
    }

}

